package com.suixinsanren.design.state.demo.demo04;

import java.util.Random;

public class HasQuarterState implements State {

    private final GumballMachine gumballMachine;
    private final Random random;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
        this.random = new Random(System.currentTimeMillis());
    }

    @Override
    public void insertQuarter() {
        System.out.println("您已经投入了硬币，不要重复投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("您的硬币已退还");
        gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("您转动了曲柄，游戏开始");
        if (gumballMachine.getCount() > 0 && random.nextInt(10) == 0) {
            System.out.println("恭喜，您是超级幸运星");
            gumballMachine.setCurrentState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setCurrentState(gumballMachine.getSoldState());
        }
    }

    @Override
    public String toString() {
        return "已投入硬币状态";
    }
}
